﻿using System.Windows;

namespace jSLCore.Client.Presentation.BaseControl.Advanced
{
    /// <summary>
    /// Dialog control, which can process request to server with DoWork Method
    /// </summary>
    public partial class DoWorkByDialog : DialogControl
    {
        private readonly bool _allowCancel;
        private string _splashScreenText;
        private object[] _pars;
        private MutliWorkEventHandler _doWorkEventHandler;
        public bool sucsOnFail;

        /// <summary>
        /// Only for designer, dont use this
        /// </summary>
        public DoWorkByDialog()
        {
            InitializeComponent();
        }

        public static void ShowDialogAndDoWork(MutliWorkEventHandler doWorkEventHandler, string splashScreenText, bool allowCancel, DataSelectedEventHandler onSucceded, bool callSuccesOnFail, BaseControl parent, params object[] pars)
        {
             var dlg = new DoWorkByDialog(doWorkEventHandler,pars,splashScreenText,allowCancel){sucsOnFail = callSuccesOnFail};
            dlg.ShowDialog(onSucceded,parent);
            
            
        }

        public static void ShowDialogAndDoWork(MutliWorkEventHandler doWorkEventHandler, string splashScreenText, bool allowCancel, DataSelectedEventHandler onSucceded, BaseControl parent, params object[] pars)
        {
            ShowDialogAndDoWork(doWorkEventHandler, splashScreenText, allowCancel, onSucceded, false, parent, pars);
        }


        private DoWorkByDialog( MutliWorkEventHandler doWorkEventHandler, object[] pars, string splashScreenText,bool allowCancel)
        {
            _allowCancel = allowCancel;
            _splashScreenText = splashScreenText;
            _pars = pars;
            _doWorkEventHandler = doWorkEventHandler;
            InitializeComponent();
        }

        public override bool AutoSize
        {
            get { return true; }
        }

        public override object CancelContent
        {
            get { return "Отмена"; }
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return _allowCancel; }
        }

        public override bool IsOkVisible
        {
            get { return false; }
        }

        public override void LoadInitialData()
        {
            DoWork(_splashScreenText,_doWorkEventHandler,(sender, args) =>
                                                  {
                                                      if(args.Error==null)
                                                          InvokeDataSelected(args.Result);
                                                      else
                                                      {
                                                          if(sucsOnFail)
                                                              InvokeDataSelected(args.Error);
                                                      }
                                                  },_pars);
        }
    }
}
